/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ecspider;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Mikael Köhlmyr | mikael@kohlmyr.com | http://www.kmyr.net
 */
public class spiderlib {
    
    public static String getPageFromUrl(String link) throws IOException{
        String str;
        String Pages = "";
        URL url = new URL(link);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        while ((str = in.readLine()) != null) {
            Pages += str;
        }
        in.close();
        return Pages;
    }

    public static ArrayList addPeopleFromPage(String doc, ArrayList people){
        ArrayList<String> others = new ArrayList<String>();
        ArrayList<Integer> points = new ArrayList<Integer>(people.size()); //initiating with an int ensures capacity
        Pattern pat = Pattern.compile("people/[a-zA-Z0-9]+");
        Matcher mat = pat.matcher(doc);
        while(mat.find()){
            String ref = mat.group().substring(7);
            if(!people.contains(ref) && !others.contains(ref)){//for tagcloud remove people.contains then run an algo that combines them and gives them points :)
                others.add(ref);
                System.out.println("Found " + ref);
            }
            else if(!others.contains(ref)){//process of elimination, this is faster because others contains fewer elements than people
                int index = people.indexOf(ref);
                points.set(index, points.get(index) + 1);
            }
        }
        return others;
    }

    public static ArrayList[] getRankings(ArrayList people){
        ArrayList<Integer> points = new ArrayList<Integer>();
        ArrayList[] array = new ArrayList[2];
        array[0] = people;
        array[1] = points;
            for(int i = 0; i < people.size(); i++){
                points.add(1);
                for(int j = i+1; j < people.size(); j++){
                    people.get(j);
                    if(people.get(j) == people.get(i)){
                        people.remove(j);
                        points.set(i, points.get(i) + 1);
                        System.out.println(people.get(i) + " had a duplicate, is now on " + points.get(i) + "points");
                    }
                }
                // for every person, see if there is a duplicate.
                // if there is a duplicate, remove it and add to the persons points
            }
        return array;
    }

    public static ArrayList spider(ArrayList people) throws IOException{
        for(int i = 0; i < people.size(); i++){
            String doc = getPageFromUrl("http://www.ecs.soton.ac.uk/people/" + people.get(i));
            people.addAll(addPeopleFromPage(doc, people));
            //System.out.println("new friends from " + people.get(i) + ">> ");
        }
        return people;
    }
}